from rest_framework.generics import ListCreateAPIView
from apps.users.models import User
# from apps.meiduo_admin.serializers.user import UserSerializer,UserAddSerializer
from apps.meiduo_admin.serializers.user import UserAddSerializer
from rest_framework.response import Response
from apps.meiduo_admin.utils import PageNum


class UserListView(ListCreateAPIView):
    #用户查询展示及分页
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            queryset = User.objects.filter(username=keyword)
        else:
            queryset = User.objects.all()
        return queryset

    #如果使用两个序列化器需要重写
    # def get_serializer_class(self):
    #     if self.request.method == 'GET':
    #         serializer_class =UserSerializer
    #         return serializer_class
    #     else:
    #         serializer_class = UserAddSerializer
    #         return serializer_class

    queryset = User.objects.all()
    serializer_class = UserAddSerializer
    pagination_class = PageNum

